package com.dongdongshop.serviceimpl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.SellerService;
import com.dongdongshop.service.TbGoodsService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class TbGoodsServiceImpl implements TbGoodsService {
    @Autowired
    private TbGoodsMapper tbGoodsMapper;
    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;
    @Autowired
    private TbItemMapper tbItemMapper;
    @Reference
    private SellerService sellerService;

    //增加商品
    @Override
    public Boolean insertTbGoodsAndDesc(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, List<TbItem> tbItems,TbItem tbItem,String sellerId,List<TbItem> tbItems2) {


        try {
            int i = tbGoodsMapper.insertSelective(tbGoods);
            TbSeller tbSeller = sellerService.selectBySellerId(sellerId);
            for (int j = 0; j < tbItems.size(); j++) {
                tbItems.get(j).setSellerId(sellerId);
                tbItems.get(j).setGoodsId(tbGoods.getId());
                tbItems.get(j).setSeller(tbSeller.getNickName());
                tbItems.get(j).setSellPoint(tbItem.getSellPoint());
                tbItems.get(j).setStockCount(tbItem.getStockCount());
                tbItems.get(j).setCategoryid(tbItem.getCategoryid());
                tbItems.get(j).setStatus(tbItem.getStatus());
                tbItems.get(j).setCreateTime(tbItem.getCreateTime());
                tbItems.get(j).setUpdateTime(tbItem.getUpdateTime());
                tbItems.get(j).setCategory(tbItem.getCategory());
                tbItems.get(j).setBrand(tbItem.getBrand());
                tbItems.get(j).setSpec(tbItems2.get(j).getSpec());
                tbItemMapper.insertSelective(tbItems.get(j));

            }

            tbGoodsDesc.setGoodsId(tbGoods.getId());
            int i2 = tbGoodsDescMapper.insertSelective(tbGoodsDesc);
            if (i > 0 && i2 > 0){
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //删除商品
    @Override
    public Boolean deleteGoods(Long[] ids) {
        try {
            for (int i = 0; i < ids.length; i++) {
                //删除三个表
                tbGoodsMapper.deleteByPrimaryKey(ids[i]);
                tbGoodsDescMapper.deleteByPrimaryKey(ids[i]);
                TbItemExample te = new TbItemExample();
                TbItemExample.Criteria criteria = te.createCriteria();
                criteria.andGoodsIdEqualTo(ids[i]);
                tbItemMapper.deleteByExample(te);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //提交审核
    @Override
    public Boolean statusUpdate(Long[] ids) {
        try {
            List<TbGoods> list = new ArrayList<>();
            for (int i = 0; i < ids.length; i++) {
                TbGoods tbGoods = new TbGoods();
                tbGoods.setId(ids[i]);
                tbGoods.setAuditStatus("1");
                list.add(tbGoods);
            }
            for (int j = 0; j < list.size(); j++) {
                tbGoodsMapper.updateByPrimaryKeySelective(list.get(j));
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //审核通过
    @Override
    public Boolean statusUpdateyes(Long[] ids) {
        try {
            List<TbItem> itemList = new ArrayList<>();
            List<TbGoods> list = new ArrayList<>();
            for (int i = 0; i < ids.length; i++) {
                TbItem item = new TbItem();
                TbGoods tbGoods = new TbGoods();
                tbGoods.setId(ids[i]);
                tbGoods.setAuditStatus("2");
                list.add(tbGoods);
                item.setGoodsId(ids[i]);
                item.setStatus("1");
                itemList.add(item);
            }
            for (int j = 0; j < list.size(); j++) {
                tbGoodsMapper.updateByPrimaryKeySelective(list.get(j));
            }
            for (int k = 0; k < itemList.size(); k++) {
                TbItemExample te = new TbItemExample();
                TbItemExample.Criteria criteria = te.createCriteria();
                criteria.andGoodsIdEqualTo(itemList.get(k).getGoodsId());
                tbItemMapper.updateByExampleSelective(itemList.get(k),te);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //驳回
    @Override
    public Boolean statusUpdateno(Long[] ids) {
        try {
            List<TbItem> itemList = new ArrayList<>();
            List<TbGoods> list = new ArrayList<>();
            for (int i = 0; i < ids.length; i++) {
                TbItem item = new TbItem();
                TbGoods tbGoods = new TbGoods();
                tbGoods.setId(ids[i]);
                tbGoods.setAuditStatus("3");
                list.add(tbGoods);
                item.setGoodsId(ids[i]);
                item.setStatus("2");
                itemList.add(item);
            }
            for (int j = 0; j < list.size(); j++) {
                tbGoodsMapper.updateByPrimaryKeySelective(list.get(j));
            }
            for (int k = 0; k < itemList.size(); k++) {
                TbItemExample te = new TbItemExample();
                TbItemExample.Criteria criteria = te.createCriteria();
                criteria.andGoodsIdEqualTo(itemList.get(k).getGoodsId());
                tbItemMapper.updateByExampleSelective(itemList.get(k),te);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //根据id查询当前商品
    @Override
    public TbGoods selectGoodsById(Long goodsid) {
        TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(goodsid);
        return tbGoods;
    }
}
